package com.sun.androidexerciseproject.function.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sun.androidexerciseproject.function.R;
import com.sun.androidexerciseproject.function.util.LogUtil;

public class ThreadTestActivity extends AppCompatActivity {
    Thread mThread;
    private Button btnThread;
    private TextView tvResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_test);
        initView();
        initData();

    }

    void initView() {
        btnThread = (Button) findViewById(R.id.btn_thread);
        btnThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mThread) {
                    mThread.start();
                }
            }
        });
        tvResult = (TextView) findViewById(R.id.tv_result);
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        });

    }

    void initData() {
        mThread = new Thread() {
            int mNum = 0;

            @Override
            public void run() {
                long threadId = Thread.currentThread().getId();
                System.out.println("threadId = " + threadId);
                LogUtil.log("threadId = " + threadId);
                mNum = mNum % 10;
                tvResult.post(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText(mNum + "");
                    }
                });
                mNum++;
            }
        };
    }

}
